const url = require('url')
const queryString = require('querystring')

function bodyParser(req, res, next) {
    if (req.method == 'GET') {
        var { query } = url.parse(req.url);
        req.body = queryString.parse(query)
        next()
    }
    if (req.method == "POST") {
        var str = ""
        req.on('data', function(chunk) {
            str += chunk
        })
        req.on('end', function() {
            req.body = JSON.parse(str)
            next()
        })
    }
}

module.exports = bodyParser